package club.emergency.wechat.dictionary.operate.consumer.controller;


import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.dictionary.operate.consumer.service.DictionaryOperateConsumer;
import club.emergency.wechat.model.dictionary.DictionaryChild;
import club.emergency.wechat.model.dictionary.DictionaryParent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jifujun
 * @since 2018-11-26
 */
@Api("字典表操作接口")
@RestController
@RequestMapping("/dictionary-operate")
public class DictionaryOperateController {

    private DictionaryOperateConsumer dictionaryOperateConsumer;

    public DictionaryOperateController(DictionaryOperateConsumer dictionaryOperateConsumer) {
        this.dictionaryOperateConsumer = dictionaryOperateConsumer;
    }

    @ApiOperation("新增父字典表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "父字典名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "keyName", value = "关键字", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/saveParent")
    public ResponseEntity saveParent(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "keyName") String keyName
    ) {
        DictionaryParent dictionaryParent = new DictionaryParent();
        dictionaryParent.setName(name);
        dictionaryParent.setKeyName(keyName);
        return new JsonConvert<ResponseEntity>().jsonDecode(dictionaryOperateConsumer.saveParent(dictionaryParent), ResponseEntity.class);
    }

    @ApiOperation("修改父字典表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "父字典名称", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/modifyParent")
    public ResponseEntity modifyParent(
            @RequestParam(value = "id") Long id,
            @RequestParam(value = "name") String name
    ) {
        DictionaryParent dictionaryParent = new DictionaryParent();
        dictionaryParent.setId(id);
        dictionaryParent.setName(name);
        return new JsonConvert<ResponseEntity>().jsonDecode(dictionaryOperateConsumer.modifyParent(dictionaryParent), ResponseEntity.class);
    }

    @ApiOperation("删除父字典表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query")
    })
    @PostMapping("/removeParent")
    public ResponseEntity removeParent(
            @RequestParam(value = "id") Long id
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(dictionaryOperateConsumer.removeParent(id), ResponseEntity.class);
    }

    @ApiOperation("新增子字典表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyName", value = "父字典关键字", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "对应的内容", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alias", value = "别名", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/saveChild")
    public ResponseEntity saveChild(
            @RequestParam(value = "keyName") String keyName,
            @RequestParam(value = "content") String content,
            @RequestParam(value = "parentId") Long parentId,
            @RequestParam(value = "alias") String alias
    ) {
        DictionaryChild dictionaryChild = new DictionaryChild();
        dictionaryChild.setKeyName(keyName);
        dictionaryChild.setContent(content);
        dictionaryChild.setAlias(alias);
        dictionaryChild.setParentId(parentId);
        return new JsonConvert<ResponseEntity>().jsonDecode(dictionaryOperateConsumer.saveChild(dictionaryChild), ResponseEntity.class);
    }

    @ApiOperation("修改子字典表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "对应的内容", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/modifyChild")
    public ResponseEntity modifyChild(
            @RequestParam(value = "id") Long id,
            @RequestParam(value = "content") String content,
            @RequestParam(value = "keyName") String keyName
    ) {
        DictionaryChild dictionaryChild = new DictionaryChild();
        dictionaryChild.setId(id);
        dictionaryChild.setContent(content);
        return new JsonConvert<ResponseEntity>().jsonDecode(dictionaryOperateConsumer.modifyChild(dictionaryChild, keyName), ResponseEntity.class);
    }

}

